/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* RDBConnectionModel.java
*
* Created on December 15, 2007, 12:17 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package db.gui;

import db.genomemap.RDBSettings;

import java.io.Serializable;

/**
 *
 * @author stewari1
 */
public class RDBConnectionModel implements Serializable {

    /** Field description */
    private static final long serialVersionUID = 42L;

    /** Field description */
    private String serverHost = null;

    /** Field description */
    private String port = "3306";

    /** Field description */
    private String userName = "root";

    /** Field description */
    private String password = "";

    /** Field description */
    private String schema = "";

    /** Field description */
    private String serverType = "";

    /** Field description */
    private String driver = "";

    /**
     * <CODE>hostName</CODE> is little different from <CODE>serverHost</CODE>
     * <CODE>hostName</CODE> is tied with the <CODE>serverType</CODE> whereas
     * <CODE>serverHost</CODE> is independent.
     * <CODE>hostName</CODE> is read-only
     */
    private String hostName = null;

    /** Creates a new instance of RDBConnectionModel */
    public RDBConnectionModel() {}

    public static void main(String[] args) {

        RDBConnectionModel model = new RDBConnectionModel();

        System.out.println(model instanceof Serializable);
    }

    public String getServerHost() {
        return serverHost;
    }

    public void setServerHost(String serverHost) {
        this.serverHost = serverHost;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public String getServerType() {
        return serverType;
    }

    public void setServerType(String serverType) {

        this.serverType = serverType;

        if (serverType.equals(RDBSettings.SERVER_TYPE_MYSQL))
            setDriver(RDBSettings.DRIVER_TYPE_MYSQL);
    }

    public String getDriver() {
        return driver;
    }

    void setDriver(String driver) {
        this.driver = driver;
    }

    /**
     * this should called only from <CODE>RDBConnection</CODE> only.
     * <CODE>serverHost<CODE>,</CODE>port</CODE> and <CODE>serverType</CODE>
     * must be set before this is called or returns <CODE>null</CODE>
     */
    public String getHostName() {

        if ((serverType.length() == 0) || (serverHost.length() == 0) || (port.length() == 0))
            return null;

        if (serverType.equals(RDBSettings.SERVER_TYPE_MYSQL)) {
            this.hostName = "jdbc:mysql://" + serverHost + ":" + port + "/mysql";
        }

        return hostName;
    }
}
